﻿/* ************************************************************************************* 
 * CREATED BY:          GIAPCM
 * CREATED DATE:        10/02/2009
 * PROJECT:             MC ENT WEBSITE PROJECT
 * PURPOSE:             
 * MODIFIED BY:         None
 * MODIFIED DATE:       None
 * MODIFIED REASON:     None
 * NOTES:               None
 **************************************************************************************/
using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using Telerik.Web.UI.Grid;
using MCWEBSITE.ENTITY;
using MCWEBSITE.BUL;
using System.IO;

public partial class Modules_WebsiteBackEnd_wucProductsList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.LoadFilterSection();
            //this.BindGridviewList();
        }
    }

    // ============================================================================================================
    #region "Event Declaration : RadGridItemsSelected"

    public delegate void RadGridItemSelectedEventHandler(object sender, RadGridItemsSelectedEventArgs e);
    public event RadGridItemSelectedEventHandler RadGridItemsSelected;

    protected virtual void OnRadGridItemsSelected(RadGridItemsSelectedEventArgs e)
    {
        if (RadGridItemsSelected != null) RadGridItemsSelected(this, e);
    }

    #endregion

    // ============================================================================================================
    #region "Event Declaration : RadGridItemsDeleted"

    public delegate void RadGridItemDeletedEventHandler(object sender, RadGridItemsDeletedEventArgs e);
    public event RadGridItemDeletedEventHandler RadGridItemsDeleted;

    protected virtual void OnRadGridItemsDeleted(RadGridItemsDeletedEventArgs e)
    {
        if (RadGridItemsDeleted != null) RadGridItemsDeleted(this, e);
    }

    #endregion

    // ============================================================================================================
    #region "Event Declaration : RadGridItemEdited"

    public delegate void RadGridItemEditedEventHandler(object sender, RadGridItemEditedEventArgs e);
    public event RadGridItemEditedEventHandler RadGridItemEdited;

    protected virtual void OnRadGridItemEdited(RadGridItemEditedEventArgs e)
    {
        if (RadGridItemEdited != null) RadGridItemEdited(this, e);
    }

    #endregion

    // ============================================================================================================
    #region "Event Declaration : RadGridItemsSubmitted"

    public delegate void RadGridItemSubmittedEventHandler(object sender, RadGridItemsSubmittedEventArgs e);
    public event RadGridItemSubmittedEventHandler RadGridItemsSubmitted;

    protected virtual void OnRadGridItemsSubmitted(RadGridItemsSubmittedEventArgs e)
    {
        if (RadGridItemsSubmitted != null) RadGridItemsSubmitted(this, e);
    }

    #endregion

    // ============================================================================================================
    // ============================================================================================================
    #region "Event Declaration : RadGridItemsUnpublished"

    public delegate void RadGridItemUnpublishedEventHandler(object sender, RadGridItemUnpublishedEventArgs e);
    public event RadGridItemUnpublishedEventHandler RadGridItemUnpublished;

    protected virtual void OnRadGridItemUnpublished(RadGridItemUnpublishedEventArgs e)
    {
        if (RadGridItemUnpublished != null) RadGridItemUnpublished(this, e);
    }

    #endregion

    // ============================================================================================================
    #region "Control Events"

    protected void rgrdGridDataList_PageIndexChanged(object source, Telerik.Web.UI.GridPageChangedEventArgs e)
    {
        rgrdGridDataList.CurrentPageIndex = e.NewPageIndex;
        this.BindGridviewList();
    }

    protected void rgrdGridDataList_ItemCommand(object source, GridCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "RowClick":
                RadGridItemsSelectedEventArgs objSelectedEventArg = null;
                int SelectedItemsCount = rgrdGridDataList.MasterTableView.GetSelectedItems().Length;
                objSelectedEventArg = new RadGridItemsSelectedEventArgs(SelectedItemsCount);
                // Fire event
                OnRadGridItemsSelected(objSelectedEventArg);
                break;
            case "RowEdit":
                // Set edit item back to normal
                RadGridItemEditedEventArgs objEditedEventArg = null;
                int ItemID = Convert.ToInt32(rgrdGridDataList.MasterTableView.Items[e.Item.ItemIndex].KeyValues.Split('\"')[1]);
                objEditedEventArg = new RadGridItemEditedEventArgs(Convert.ToString(ItemID));
                rgrdGridDataList.MasterTableView.Items[e.Item.ItemIndex].Edit = false;
                // Bind grid again
                this.BindGridviewList();
                // Fire event
                OnRadGridItemEdited(objEditedEventArg);
                break;
            case "RowDelete":
                // Delete row
                RadGridItemsDeletedEventArgs objDeletedEventArg = null;
                int DeletedItemsCount = DeleteSelectedItems(Convert.ToInt32(rgrdGridDataList.MasterTableView.Items[e.Item.ItemIndex].KeyValues.Split('\"')[1]));
                objDeletedEventArg = new RadGridItemsDeletedEventArgs(DeletedItemsCount, 0);
                // Bind grid again
                this.BindGridviewList();
                // Fire event
                OnRadGridItemsDeleted(objDeletedEventArg);
                break;
            case "RowPublish":
                // Submit row
                RadGridItemsSubmittedEventArgs objSubmittedEventArg = null;
                int SubmittedItemsCount = SubmitSelectedItems(Convert.ToInt32(rgrdGridDataList.MasterTableView.Items[e.Item.ItemIndex].KeyValues.Split('\"')[1]));
                //Submit 1 record on List button
                objSubmittedEventArg = new RadGridItemsSubmittedEventArgs(SubmittedItemsCount, 0);
                // Bind grid again
                this.BindGridviewList();
                // Fire event
                OnRadGridItemsSubmitted(objSubmittedEventArg);
                break;
            case "RowUnPublish":
                RadGridItemUnpublishedEventArgs objUnpublishedEventArg = null;
                ItemID = Convert.ToInt32(rgrdGridDataList.MasterTableView.Items[e.Item.ItemIndex].KeyValues.Split('\"')[1]);
                int UnpublishedItemsCount = UnpublishSelectedItems(Convert.ToInt32(rgrdGridDataList.MasterTableView.Items[e.Item.ItemIndex].KeyValues.Split('\"')[1]));
                //Submit 1 record on List button
                objUnpublishedEventArg = new RadGridItemUnpublishedEventArgs(UnpublishedItemsCount);
                // Bind grid again
                this.BindGridviewList();
                // Fire event
                OnRadGridItemUnpublished(objUnpublishedEventArg);
                break;
        }
    }
    #endregion

    #region "Public function/procedure and utilities"

    protected void LoadFilterSection()
    {
        rcbLanguage.Items.Clear();
        List<websiteLanguageInfo> LanguageList = websiteLanguageBUL.GetAll();
        websiteLanguageInfo objSelectAll = new websiteLanguageInfo();
        objSelectAll.LanguageCode = "TOÀN BỘ";
        objSelectAll.languageName = "TOÀN BỘ";
        objSelectAll.Description = "";
        LanguageList.Add(objSelectAll);
        rcbLanguage.DataSource = LanguageList;
        rcbLanguage.DataTextField = "languageName";
        rcbLanguage.DataValueField = "LanguageCode";
        rcbLanguage.DataBind();
        rcbLanguage.SelectedIndex = rcbLanguage.FindItemIndexByValue("TOÀN BỘ");
    }

    public void BindGridviewList()
    {
        this.BindGridviewList(rcbActive.SelectedValue, rcbLanguage.SelectedValue);
    }

    /// <summary>
    /// Bind data to Gridview
    /// </summary>
    /// <param name="ClassBlockID"></param>
    public void BindGridviewList(string status, string strLanguage)
    {
        if (strLanguage != "TOÀN BỘ")
        {            
            rgrdGridDataList.DataSource = websiteProductBUL.GetByLanguage(status, strLanguage);
            rgrdGridDataList.DataBind();
            this.SetNewsGridStatus(rcbActive.SelectedValue);
        }
        else
        {
            rgrdGridDataList.DataSource = websiteProductBUL.GetByStatus(status);
            rgrdGridDataList.DataBind();
            this.SetNewsGridStatus(rcbActive.SelectedValue);
        }
    }

    /// <summary>
    /// Delete record by list of ID
    /// </summary>
    /// <returns></returns>
    public void DeleteSelectedItems(ArrayList SelectedItemList)
    {
        int TotalSelectedItems = SelectedItemList.Count;
        int DeletedItems = 0;
        for (int i = 0; i < SelectedItemList.Count; i++)
        {
            DeletedItems = DeletedItems + DeleteSelectedItems(Convert.ToInt32(SelectedItemList[i]));
        }
        RadGridItemsDeletedEventArgs objDeletedEventArg = null;
        objDeletedEventArg = new RadGridItemsDeletedEventArgs(DeletedItems, TotalSelectedItems - DeletedItems);
        // Bind grid again
        this.BindGridviewList();
        // Fire event
        OnRadGridItemsDeleted(objDeletedEventArg);
    }

    /// <summary>
    /// Delete record by ID
    /// </summary>
    /// <param name="SubjectID"></param>
    /// <returns></returns>
    public int DeleteSelectedItems(int SelectedItemID)
    {
        websiteProductInfo objNews = websiteProductBUL.Load(SelectedItemID);
        // Delete Product directory
        DirectoryInfo ResourceDirectory = new DirectoryInfo(Server.MapPath(objNews.Image));
        if (ResourceDirectory.Exists)
            ResourceDirectory.Delete(true);
        return websiteProductBUL.Remove(SelectedItemID);
    }

    /// <summary>
    /// Submitup record by list of ID
    /// </summary>
    /// <returns></returns>
    public void SubmitSelectedItems(ArrayList SelectedItemList)
    {
        int TotalSelectedItems = SelectedItemList.Count;
        int SubmittedItems = 0;
        for (int i = 0; i < SelectedItemList.Count; i++)
        {
            SubmittedItems = SubmittedItems + SubmitSelectedItems(Convert.ToInt32(SelectedItemList[i]));
        }
        RadGridItemsSubmittedEventArgs objSubmittedEventArg = null;
        objSubmittedEventArg = new RadGridItemsSubmittedEventArgs(SubmittedItems, TotalSelectedItems - SubmittedItems);
        // Bind grid again
        this.BindGridviewList();
        // Fire event
        OnRadGridItemsSubmitted(objSubmittedEventArg);
    }

    /// <summary>
    /// Submitup record by ID
    /// </summary>
    /// <param name="ModuleInstanceID"></param>
    /// <returns></returns>
    public int SubmitSelectedItems(int SelectedItemID)
    {
        return websiteProductBUL.publish(SelectedItemID);
    }

    /// <summary>
    /// Unpublish record by list of ID
    /// </summary>
    /// <returns></returns>
    public void UnpublishSelectedItems(ArrayList SelectedItemList)
    {
        int TotalSelectedItems = SelectedItemList.Count;
        int UnpublishedItems = 0;
        for (int i = 0; i < SelectedItemList.Count; i++)
        {
            UnpublishedItems = UnpublishedItems + UnpublishSelectedItems(Convert.ToInt32(SelectedItemList[i]));
        }
        RadGridItemUnpublishedEventArgs objUnbpublishedEventArg = null;
        objUnbpublishedEventArg = new RadGridItemUnpublishedEventArgs(UnpublishedItems);
        // Bind grid again
        this.BindGridviewList();
        // Fire event
        OnRadGridItemUnpublished(objUnbpublishedEventArg);
    }

    /// <summary>
    /// Unpublish record by ID
    /// </summary>
    /// <param name="ModuleInstanceID"></param>
    /// <returns></returns>
    public int UnpublishSelectedItems(int SelectedItemID)
    {
        return websiteProductBUL.unpublish(SelectedItemID);
    }

    /// <summary>
    /// excute filter gridview
    /// </summary>
    public void FilterGridView()
    {
        this.BindGridviewList();
    }

    /// <summary>
    /// Refresh gridview
    /// </summary>
    public void RefreshGridView()
    {
        this.BindGridviewList();
    }

    /// <summary>
    /// Get selected item in grid
    /// </summary>
    /// <returns></returns>
    public ArrayList GridSelectedItems()
    {
        int countCheckedItem = rgrdGridDataList.MasterTableView.GetSelectedItems().Length;
        ArrayList SelectedArray = new ArrayList();

        foreach (GridDataItem _GridDataItem in rgrdGridDataList.MasterTableView.GetSelectedItems())
        {
            SelectedArray.Add(_GridDataItem.KeyValues.Split('\"')[1]);
        }
        return SelectedArray;
    }

    protected void SetNewsGridStatus(string ActiveStatus)
    {
        //Da phat hanh
        if (ActiveStatus == "1")
        {

            rgrdGridDataList.Columns[9].Visible = false;
            rgrdGridDataList.Columns[10].Visible = true;
        }
        //Cho phat hanh
        if (ActiveStatus == "0")
        {
            rgrdGridDataList.Columns[9].Visible = true;
            rgrdGridDataList.Columns[10].Visible = false;

        }

    }
    #endregion
}
